/*
 * @Description:
 * @Version: 2.0
 * @Author: godless
 * @Date: 2024-06-25 19:18:08
 * @LastEditors: godless
 * @LastEditTime: 2024-06-30 00:21:46
 */
import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "@/layout";

Vue.use(VueRouter);

// 解决重定向问题
const VueRouterPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch((err) => err);
};

// 路由白名单
const constantRoutes = [
  {
    path: "/",
    component: Layout,
    redirect: "/home",
    hidden: false,
    children: [
      {
        path: "home",
        name: "Home",
        component: () => import("@/views/home/index.vue"),
        meta: { title: "首页", icon: "el-icon-s-home" },
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    hidden: true,
  },
  {
    path: "/example",
    component: Layout,
    name: "Example",
    meta: { title: "示例", icon: "el-icon-menu" },
    redirect: "/example/children1",
    children: [
      {
        path: "children1",
        name: "Children1",
        component: () => import("@/views/example/children1/index.vue"),
        meta: { title: "示例1" },
      },
      {
        path: "children2",
        name: "Children2",
        redirect: "children2-1",
        component: () => import("@/views/example/children2/index.vue"),
        meta: { title: "示例2" },
        children: [
          {
            path: "children2-1",
            name: "Children2-Child1",
            component: () =>
              import("@/views/example/children2/children2Child1/index.vue"),
            meta: { title: "示例2-1" },
            hidden: false,
          },
          {
            path: "children2-2",
            name: "Children2-Child2",
            component: () =>
              import("@/views/example/children2/children2Child2/index.vue"),
            meta: { title: "示例2-2" },
            hidden: false,
          },
          {
            path: "children2-3",
            name: "Children2-Child3",
            component: () =>
              import("@/views/example/children2/children2Child3/index.vue"),
            meta: { title: "示例2-3" },
            hidden: false,
          },
        ],
      },
      {
        path: "children3",
        name: "Children3",
        component: () => import("@/views/example/children3/index.vue"),
        meta: { title: "示例3" },
      },
    ],
  },
];

const createRouter = () =>
  new VueRouter({
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// 重置路由
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
